﻿#pragma once

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Output.H>
#include <FL/Fl_Return_Button.H>
#include "UartWriteFile.h"
#include "MyQueue.h"

#define UART_PROT_SIZE 16
#define UART_BAUD_SIZE 16

typedef struct __uart_infor_t
{
	char uart_port[UART_PROT_SIZE];
	char uart_baud[UART_BAUD_SIZE];

}uart_infor_t;

void StartUart(Fl_Widget* w, void* p);

void CloseUart(Fl_Widget* w, void* p);

/* Stop uart */
void StopUart(Fl_Widget* w, void* p);


void SaveFileCbk(Fl_Widget* w, void* p);

void CloseFileCbk(Fl_Widget* w, void* p);

void SendDataCbk(Fl_Widget* w, void* p);

class MainWidget:public Fl_Window
{
public:
	MainWidget(int w, int h, const char* title = 0) :Fl_Window(300, 300)
	{
		this->begin();

		Prot = new Fl_Input(30, 70, 60, 20, "Port");	//child 0   : 1st widget
		Baud = new Fl_Input(160, 70, 60, 20, "Baud");	//child 1   : 1st widget
		SendDataWidget = new Fl_Input(20, 10, 200, 50);		//child 2   : 1st widget

		//SendButton = new Fl_Button(230, 20, 70, 30, "Send");

		SendButton = new Fl_Return_Button(230, 20, 70, 30, "Send");

		Open_UART = new Fl_Button(20, 100, 60, 30, "STARAT");

	//  Stop_UART = new Fl_Button(90, 100, 60, 30, "STOP");

	//	Close_UART = new Fl_Button(160, 100, 60, 30, "EXIT");

		SaveToFile = new Fl_Button(20, 140, 60, 30, "Save");

		CloseFile = new Fl_Button(160, 140, 60, 30, "Clean");

		PortInfo = new Fl_Button(20, 180, 60, 30, "PortInfo");

		
		this->end();

		get_uart_port(uart_infor.uart_port);

		get_uart_baud(uart_infor.uart_baud);

		SendButton->callback(SendDataCbk, NULL);

		Open_UART->callback(StartUart, (void*)&uart_infor);

		//Stop_UART->callback(StopUart, NULL);

		//Close_UART->callback(CloseUart, (void*)&uart_infor);

		SaveToFile->callback(SaveFileCbk, ".\\Uart_log.txt");

		CloseFile->callback(CloseFileCbk, NULL);

		this->show();
	};

	void get_uart_information(void);

	void get_uart_port(char* uart_port);

	void get_uart_baud(char* uart_baud);
	

	~MainWidget()
	{
		delete(SendDataWidget);
		delete(Prot);
		delete(Baud);
		delete(SendButton);
		delete(Open_UART);
		//delete(Stop_UART);
		//delete(Close_UART);
		delete(SaveToFile);
		delete(CloseFile);
	};

private:
	Fl_Button*  Open_UART = NULL;		//Open uart
	Fl_Button*  Close_UART = NULL;		//Close uart
	Fl_Button*  Stop_UART = NULL;
	Fl_Button*  SaveToFile = NULL;		//Save data to file.
	Fl_Button*  CloseFile = NULL;		//cloase save file.
	Fl_Button*  SendButton = NULL;		//cloase save file.
	Fl_Button*  PortInfo = NULL;
	
 	Fl_Input* SendDataWidget = NULL;	//Send data go out.
	Fl_Input* Prot = NULL;
	Fl_Input* Baud = NULL;
	
	uart_infor_t uart_infor;
	
};

